import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import Layout2 from '@/layout/index2'
import LayoutSpace from '@/layout/space'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

export const constantRoutes = [
  // login
  {
    path: '/user2',
    component: Layout2,
    children: [
      {
        path: '/login',
        component: () => import('@/views/login'),
        meta: { title: '登录', left: false }
      },
      {
        path: '/user/loginmobile',
        component: () => import('@/views/login/withMobile'),
        name: 'loginmobile',
        meta: { title: '短信验证码登录' }
      },
      {
        path: '/user/register',
        component: () => import('@/views/login/register'),
        name: 'register',
        meta: { title: '注册' }
      },
      {
        path: '/user/resetpsw',
        component: () => import('@/views/login/resetPsw'),
        name: 'resetpsw',
        meta: { title: '重置密码' }
      }
    ]
  },

  // home
  {
    path: '/',
    component: Layout,
    redirect: '/home/home',
    children: [
      {
        path: 'home',
        component: () => import('@/views/home/index'),
        name: 'Home',
        children: [
          {
            path: '/home/home',
            component: () => import('@/views/home/home'),
            name: 'home',
            meta: { title: 'home', left: false, help: false }
          },
          {
            path: '/home/message',
            component: () => import('@/views/home/message'),
            name: 'message',
            meta: { title: 'message', left: false }
          },

          {
            path: '/home/me',
            component: () => import('@/views/home/me'),
            name: 'me',
            meta: {
              title: 'Me',
              left: false,
              right: { path: '/user/setting', icon: 'setting-o' }
            }
          }
        ]
      }
    ]
  },

  // user
  {
    path: '/user',
    component: Layout,
    children: [
      {
        path: '/user/setting',
        component: () => import('@/views/me/setting'),
        meta: { title: '设置' }
      },
      {
        path: '/user/changepsw',
        component: () => import('@/views/me/changePsw'),
        meta: { title: '修改密码' }
      },
      {
        path: '/user/resetpsw',
        component: () => import('@/views/me/resetPsw'),
        meta: { title: '重置密码' }
      }
    ]
  },

  // object
  {
    path: '/object',
    component: Layout,
    children: [
      {
        path: '/object/list',
        component: () => import('@/views/object/list'),
        meta: { title: '' }
      },
      {
        path: '/object/view',
        component: () => import('@/views/object/view'),
        meta: { title: '' }
      },
      {
        path: '/object/form',
        component: () => import('@/views/object/form'),
        meta: { title: '' }
      }
    ]
  },

  // erp
  {
    path: '/erp',
    component: Layout,
    children: [
      {
        path: '/erp/menus',
        component: () => import('@/views/erp/menuPage'),
        meta: { title: '' }
      },
      {
        path: '/erp/contacts',
        component: () => import('@/views/erp/contacts'),
        meta: { title: '联系人' }
      },
      {
        path: '/erp/sales',
        component: () => import('@/views/erp/sales'),
        meta: { title: '销售管理' }
      },

      // product
      {
        path: '/erp/product',
        component: LayoutSpace,
        children: [
          {
            path: '/erp/product/list',
            component: () => import('@/views/product/products'),
            meta: { title: '产品列表' }
          },
          {
            path: '/erp/product/view',
            component: () => import('@/views/product/product'),
            meta: { title: '产品详情' }
          },
          {
            path: '/erp/product/form',
            component: () => import('@/views/product/form'),
            meta: { title: '产品' }
          }
        ]
      },

      // partner
      {
        path: '/erp/partner',
        component: LayoutSpace,
        children: [
          {
            path: '/erp/partner/list',
            component: () => import('@/views/partner/list'),
            meta: { title: '客户列表' }
          },
          {
            path: '/erp/partner/view',
            component: () => import('@/views/partner/view'),
            meta: { title: '客户详情' }
          },
          {
            path: '/erp/partner/form',
            component: () => import('@/views/partner/form'),
            meta: { title: '客户' }
          }
        ]
      }

      // {
      //   path: '/po/po',
      //   component: () => import('@/views/po/po'),
      //   meta: { title: 'po' }
      // },
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // for mobile
  // {
  //   path: '/app',
  //   component: () => import('@/views/home'),
  //   hidden: true
  // },
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
